package logmanager.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.rmi.RemoteException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;

import logmanager.menu.ActionMenuCliked;
import logmanager.menu.MenuField;
import logmanager.proxy.ServerLogManager;
import logmanager.view_connection.ActionDisconnect;
import logmanager.view_connection.ConnectionView;

//classe de la fenetre principal
public class Principal extends JFrame{
	private static final long serialVersionUID = -5714454088204455817L;
	public LabeledTextField textRecherche;
	public JComboBox searchCombo, colorTypeSearch;
	public JRadioButton typeSearch1, typeSearch2;
	public JButton buttonDisconnect, buttonSearch, buttonNext, buttonPrev;
	public JLabel infoMaxDate, infoMinDate, infoLogSize;
	public PrincipalView pv;
	public ServerLogManager logManager;
	public boolean disconnected = true;
	public MenuField menu;
	private int selectedIndex=-1;

	public Principal() {
		this.setTitle("I-Discovery Log Viewer");
		this.setSize(1000, 667);
		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}catch (Exception e) {
			e.printStackTrace();
		}
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		//menu
		menu = new MenuField(new String[][] {
				{"Fichier","Ouvrir", "Deconnecter","-","Quitter"},
				{"Log","Fermer","Fermer tous"},
				{"Option","Propriete"}
		});

		menu.setMnemonic(0, 'F');

		setJMenuBar(menu);
		menu.addActionListener(new ActionMenuCliked(this));

		menu.setEnabled(0, 0, false);
		menu.setEnabled(0, 1, false);

		menu.setEnabled(1, 0, false);
		menu.setEnabled(1, 1, false);

		menu.setEnabled(2, 0, false);

		//zone de connection
		add(new ConnectionView(this, logManager));

		setVisible(true);
		setLocationRelativeTo(null);
	}

	//action de deconnexion
	public void disconnect(){
		setContentPane(new ConnectionView(this, logManager));
		setVisible(true);
		disconnected = true;

		menu.setEnabled(0, 0, false);
		menu.setEnabled(0, 1, false);

		menu.setEnabled(1, 0, false);

		menu.setEnabled(2, 0, false);

		buttonNext.setEnabled(false);
		buttonPrev.setEnabled(false);

		pv.closeAllLog();
		logManager = null;
	}

	//initialisation de la fenetre principale
	public void initPrincipalView(){
		JPanel zone = new JPanel(new BorderLayout());
		zone.setOpaque(false);

		//zone du tableau
		pv = new PrincipalView(this, logManager);
		if(pv.init() == 1)
			return;
		zone.add("Center", pv);		

		//zone du text de recherche
		JLabel labelField = new JLabel("Champ : ");
		searchCombo = new JComboBox();

		textRecherche = new LabeledTextField("Valeur", 15);
		JPanel recherchePanel = new JPanel();
		recherchePanel.setLayout(new FlowLayout(FlowLayout.LEFT));

		buttonSearch = new JButton("Recherche");

		recherchePanel.add(labelField); recherchePanel.add(searchCombo);
		recherchePanel.add(textRecherche);
		recherchePanel.add(buttonSearch);
		zone.add("North", recherchePanel);

		textRecherche.t.addKeyListener(new ActionKeySearch(pv, textRecherche.t));
		searchCombo.addKeyListener(new ActionKeySearch(pv, textRecherche.t));
		buttonSearch.addMouseListener(new ActionSearch(pv, textRecherche.t));

		searchCombo.setEnabled(false);
		textRecherche.t.setEnabled(false);
		buttonSearch.setEnabled(false);
		//zone du bouton quitter
		buttonDisconnect = new JButton("Deconnecter");
		buttonDisconnect.addMouseListener(new ActionDisconnect(this));
		buttonNext = new JButton("Suivant");
		buttonNext.addMouseListener(new ActionNext(this));
		buttonPrev = new JButton("Precedant");
		buttonPrev.addMouseListener(new ActionPrev(this));

		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

		JPanel infoPanel = new JPanel();
		infoMaxDate = new JLabel();
		infoMinDate = new JLabel();
		infoLogSize = new JLabel();
		infoPanel.add(infoMaxDate);
		infoPanel.add(infoMinDate);
		infoPanel.add(infoLogSize);

		JPanel southPanel = new JPanel(new BorderLayout());
		southPanel.add("West", infoPanel);
		southPanel.add("East", buttonPanel);
		///////////////////////////////////////////////////////////////////////////////////

		buttonPanel.add(buttonPrev);
		buttonPanel.add(buttonNext);
		buttonPanel.add(buttonDisconnect);
		zone.add("South", southPanel);
		buttonPrev.setEnabled(false);
		buttonNext.setEnabled(false);

		setContentPane(zone);
		pv.applyProperties();
	}

	//action de connexion
	public void connected(ServerLogManager logs){
		this.logManager = logs;
		initPrincipalView();
		setVisible(true);
		disconnected = false;
		menu.setEnabled(0, 0, true);
		menu.setEnabled(0, 1, true);
		menu.setEnabled(2, 0, true);
	}

	public int getSelectedIndex() {
		return selectedIndex;
	}

	public void setSelectedIndex(int selectedIndex) {
		this.selectedIndex = selectedIndex;
		searchCombo.removeAllItems();
		loadChange();
	}

	//action de changement d'onglet
	private void loadChange(){
		buttonNext.setEnabled(false);
		buttonPrev.setEnabled(false);
		try {
			String [] header = logManager.getDataHeader(selectedIndex);
			searchCombo.addItem("Tous les champs");
			for (int i = 0; i < header.length; i++) {
				searchCombo.addItem(header[i]);
			}
			pv.loadChange(selectedIndex);
			textRecherche.t.setText(pv.oldFilterValue.get(selectedIndex));
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	//programme principal
	public static void main(String[] args) {
		new Principal();
	}	

}
